<template>
    <div class="cmt-container">
        <h3>发表评论---- {{id}}</h3>
        <hr>
        <textarea placeholder="请输入要BB的内容（最多吐槽120字）" maxlength="120" v-model="content"></textarea>
        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>

        <div class="cmt-list">
            <div class="cmt-item" v-for="(item, i) in comments" :key="item.add_time">
                <div class="cmt-title">第{{ i+1 }}楼用户：{{ item.user_name }}发表时间:{{item.add_time | formatTime }}</div>
                <div class="cmt-content">{{ item.content === 'undefined' ? '此用户很懒，嘛都没说' : item.content }}</div>
            </div>            
        </div>

        <mt-button type="danger" size="large" plain @click="getComment">加载更多</mt-button>
    </div>
</template>

<script>
import { Toast } from 'mint-ui';

export default {
    data() {
        return {
            // id: this.$route.params.id,
            pageIndex: 1,
            comments: [],
            content: ''
        };
    },
    created () {
        this.getComment();
    },
    props: ["id"],
    methods: {
        getComment() {            
            this.$http.get('api/getcomments/' + this.id + "?pageindex=" + this.pageIndex)
            .then(result => {                
                if (result.body.status === 0) {
                    // 获取成功
                    console.log(result.body)
                    if (result.body.message.length == 0) {
                        // 查询完成，加载更多按钮置灰
                    }
                    else {
                        // 用push不正确？为啥！
                        // this.comments.push(result.body.message);
                        this.comments = this.comments.concat(result.body.message);

                        console.log(this.comments);
                        this.pageIndex++;
                    }
                }
                
            })
        },
        // 添加评论
        postComment() {
            if (this.content.trim().length === 0) {
                // 发表评论为空
                Toast("评论内容为空!");
                return;
            }

            // 发送后端请求
            this.$http.post('api/postcomment/' + this.id, {
                content: this.content
            })
            .then(result => {
                this.content = '';
                if (result.body.status === 0) {
                    Toast('添加评论成功');
                    // pageIndex 恢复为1，重新加载评论？
                    this.pageIndex = 1;
                    this.comments = [];
                    this.getComment();
                }
                else {
                    Toast('添加评论失败!');
                }
            })
        }
    },
}
</script>

<style lang="scss" scoped>
    .cmt-container {
        h3 {
            font-size: 18px;
        }

        textarea {
            font-size: 14px;
            height: 85px;
            margin: 0;
        }

        .cmt-list {
            margin: 5px 0;
            .cmt-item {
                font-size: 13px;
                .cmt-title {
                    line-height: 30px;
                    background-color: #ccc;
                }
                .cmt-content {
                    line-height: 35px;
                    text-indent: 2em;
                }
            }
            
        }
    }
</style>
